package team.bailan.service.impl;

import org.springframework.stereotype.Service;
import team.bailan.common.config.ReturnResponse;
import team.bailan.common.process.dealcard.DealCards;
import team.bailan.common.process.dealcard.InitialCards;
import team.bailan.entity.dto.PlayerInfDTO;
import team.bailan.entity.dto.RoomInfDTO;
import team.bailan.entity.vo.InitialCardVO;
import team.bailan.service.CardService;

@Service
public class CardServiceImpl implements CardService {
    @Override
    public synchronized ReturnResponse<InitialCardVO> getInitialCards(PlayerInfDTO playerInfDTO) {
        int roomId = playerInfDTO.getRoomId();
        int playerPosition = playerInfDTO.getPlayerPosition();
        InitialCardVO initialCardVO = new InitialCardVO();
        if (InitialCards.initialCardsMap.containsKey("room_" + roomId)) {
            initialCardVO.setInitialCards(InitialCards.initialCardsMap.get("room_" + roomId).getCardsByPlayerPosition(playerPosition));
        } else {
            DealCards dealCards = new DealCards();
            dealCards.dealCardsProcess();
            InitialCards.initialCardsMap.put("room_" + roomId, dealCards);
            initialCardVO.setInitialCards(dealCards.getCardsByPlayerPosition(playerPosition));
        }
        return ReturnResponse.OK(initialCardVO);
    }

    @Override
    public ReturnResponse<InitialCardVO> getLordCards(PlayerInfDTO playerInfDTO) {
        int roomId = playerInfDTO.getRoomId();
        InitialCardVO initialCardVO = new InitialCardVO();
        if (InitialCards.initialCardsMap.containsKey("room_" + roomId)) {
            initialCardVO.setInitialCards(InitialCards.initialCardsMap.get("room_" + roomId).getLordCards());
            return ReturnResponse.OK(initialCardVO);
        } else {
            DealCards dealCards = new DealCards();
            dealCards.dealCardsProcess();
            InitialCards.initialCardsMap.put("room_" + roomId, dealCards);
            initialCardVO.setInitialCards(dealCards.getLordCards());
            return ReturnResponse.OK(initialCardVO);
        }
    }

    @Override
    public ReturnResponse<String> refresh(RoomInfDTO roomInfDTO) {
        int roomId = roomInfDTO.getRoomId();
        InitialCards.initialCardsMap.get("room_" + roomId).dealCardsProcess();
        return ReturnResponse.OK("重新发牌成功");
    }
}
